import { Body, Controller, Delete, Get, Post, Put} from '@nestjs/common'
import { BrandService } from './brand.service'
@Controller('brand')
export class BrandController {
    constructor(private readonly BrandService: BrandService) { }

    @Get()
    get() {
        return {
            code: 200,
            msg: "获取成功"
        }
    }

    @Post('addBrand')
    async addBrand(@Body() body: any) {
        this.BrandService.addBrand({ brandName: body.brandName, brandDesc: body.brandDesc})
        return {
            code: 200,
            msg: "添加成功"
        }
    }

    @Post("getBrand")
    async getBrand(@Body() body: any) {
        const { brandName } = body;
        console.log(brandName)
        let searchList = [];
        if (brandName) {
            searchList.push({ brandName: { $regex: brandName } });
        }


        let data = await this.BrandService.getBrand(searchList)

        let data2 = await this.BrandService.getBrand([])

        let brandNameList = [{
            value: '',
            label: '请选择',
        }]
        
        data2.forEach(item => {
            brandNameList.push({ value: item.brandName, label: item.brandName})
        })
        return {
            code: 200,
            msg: "查询成功",
            data,
            brandNameList
        }
    } 

    @Put("updateBrand") 
    async updateBrand(@Body() body: any) {
        this.BrandService.updateBrand(body)
        return {
            code: 200,
            msg: "修改成功" 
        }
    } 

    @Delete("deleteBrand")
    async deleteBrand(@Body() body: any) {
        this.BrandService.deleteBrand(body)
        return {
            code: 200,
            msg: "删除成功"
        }
    }

}
